﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using QLCONGNOLIBS.Securities;
using QLCONGNOAPPs.UserForms.Customers;
using QLCONGNOLIBS.Controllers;
using DevExpress.XtraEditors;


namespace QLCONGNOAPPs.UserControls.Customers
{
    public partial class ucCustomers : RightForms
    {            
        public ucCustomers(SqlConnection objConnection, SecurityEntity objSecurity)
                        :base(objConnection, objSecurity)
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;
            ucBaseControl1.PressNew += new EventHandler(ucBaseControl1_PressNew);
            ucBaseControl1.PressEdit += new EventHandler(ucBaseControl1_PressEdit);
            ucBaseControl1.PressDelete += new EventHandler(ucBaseControl1_PressDelete);
            ucBaseControl1.PressClose += new EventHandler(ucBaseControl1_PressClose);
            ucBaseControl1.PressRefresh += new EventHandler(ucBaseControl1_PressRefresh);
            Reload();     
        }

        void ucBaseControl1_PressRefresh(object sender, EventArgs e)
        {
            Reload();
        }
        
        void ucBaseControl1_PressClose(object sender, EventArgs e)
        {
            this.Dispose();
        }

        protected override void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            bool bFlag = false;
            if (e.Button.ButtonType == NavigatorButtonType.EndEdit)
            {                               
                if (string.IsNullOrEmpty("" + grdVCustomers.GetFocusedRowCellValue("CustomerID")))
                {
                    m_objCustomer.CustomerID = Guid.NewGuid();
                    bFlag = true; //Insert
                }
                else
                {
                    m_objCustomer.CustomerID = Guid.Parse("" + grdVCustomers.GetFocusedRowCellValue("CustomerID"));
                    bFlag = false;  //Update
                }
                m_objCustomer.CustomerName = "" + grdVCustomers.GetFocusedRowCellValue("CustomerName");
                m_objCustomer.CompanyName = "" + grdVCustomers.GetFocusedRowCellValue("CompanyName");
                m_objCustomer.Address = "" + grdVCustomers.GetFocusedRowCellValue("Address");
                m_objCustomer.PhoneNo = "" + grdVCustomers.GetFocusedRowCellValue("PhoneNo");
                m_objCustomer.Email = "" + grdVCustomers.GetFocusedRowCellValue("Email");
                m_objCustomer.CityID = "" + grdVCustomers.GetFocusedRowCellValue("CityID");
                m_objCustomer.Employee_ID = Guid.Parse("" + grdVCustomers.GetFocusedRowCellValue("Employee_ID"));
                m_objCustomer.IsActive = bool.Parse("" + grdVCustomers.GetFocusedRowCellValue("IsActive"));
                m_objCustomer.IsDeleted = bool.Parse("" + grdVCustomers.GetFocusedRowCellValue("IsDeleted"));
                decimal Creditline = 0;
                if (decimal.TryParse("" + grdVCustomers.GetFocusedRowCellValue("CreditLine"), out Creditline))
                {
                    m_objCustomer.CreditLine = Creditline;
                }
                frmCustomerDetails frm = new frmCustomerDetails(m_objCustomer, m_objConnection, m_objSecurity, bFlag);
                frm.ShowDialog();
                Reload();
            }
        }

        protected override void Reload()
        {
            LoadCustomers();
        }

        void ucBaseControl1_PressDelete(object sender, EventArgs e)
        {
            MessageBox.Show("Nghiệp vụ chưa xử lý");
        }

        void ucBaseControl1_PressEdit(object sender, EventArgs e)
        {
            //1. Lấy thông tin về bản ghi được chọn
            try
            {
                this.Cursor = Cursors.WaitCursor;
                string strCustomerID = "" + grdVCustomers.GetFocusedRowCellValue("CustomerID");
                if (!string.IsNullOrEmpty(strCustomerID))
                {                    
                    m_objCustomer.CustomerID = Guid.Parse("" + grdVCustomers.GetFocusedRowCellValue("CustomerID"));
                    m_objCustomer.CustomerName = "" + grdVCustomers.GetFocusedRowCellValue("CustomerName");
                    m_objCustomer.CompanyName = "" + grdVCustomers.GetFocusedRowCellValue("CompanyName");
                    m_objCustomer.Address = "" + grdVCustomers.GetFocusedRowCellValue("Address");
                    m_objCustomer.PhoneNo = "" + grdVCustomers.GetFocusedRowCellValue("PhoneNo");
                    m_objCustomer.Email = "" + grdVCustomers.GetFocusedRowCellValue("Email");
                    m_objCustomer.CityID = "" + grdVCustomers.GetFocusedRowCellValue("CityID");
                    m_objCustomer.Employee_ID = Guid.Parse("" + grdVCustomers.GetFocusedRowCellValue("Employee_ID"));
                    m_objCustomer.IsActive = bool.Parse("" + grdVCustomers.GetFocusedRowCellValue("IsActive"));
                    m_objCustomer.IsDeleted = bool.Parse("" + grdVCustomers.GetFocusedRowCellValue("IsDeleted"));
                    if (!string.IsNullOrEmpty("" + grdVCustomers.GetFocusedRowCellValue("CreditLine")))
                        m_objCustomer.CreditLine = decimal.Parse("" + grdVCustomers.GetFocusedRowCellValue("CreditLine"));
                    else
                        m_objCustomer.CreditLine = 0;
                    if (!string.IsNullOrEmpty("" + grdVCustomers.GetFocusedRowCellValue("Due")))
                        m_objCustomer.Due = int.Parse("" + grdVCustomers.GetFocusedRowCellValue("Due"));
                    else
                        m_objCustomer.Due = 0;
                    if (!string.IsNullOrEmpty("" + grdVCustomers.GetFocusedRowCellValue("Gov")))
                        m_objCustomer.Gov = "" + grdVCustomers.GetFocusedRowCellValue("Gov");
                    else
                        m_objCustomer.Gov = "";
                    if (!string.IsNullOrEmpty("" + grdVCustomers.GetFocusedRowCellValue("Fax")))
                        m_objCustomer.Fax = "" + grdVCustomers.GetFocusedRowCellValue("Fax");
                    else
                        m_objCustomer.Fax = "";
                    if (!string.IsNullOrEmpty("" + grdVCustomers.GetFocusedRowCellValue("BankAccount")))
                        m_objCustomer.BankAccount = "" + grdVCustomers.GetFocusedRowCellValue("BankAccount");
                    else
                        m_objCustomer.BankAccount = "";
                    if (!string.IsNullOrEmpty("" + grdVCustomers.GetFocusedRowCellValue("BankName")))
                        m_objCustomer.BankName = "" + grdVCustomers.GetFocusedRowCellValue("BankName");
                    else
                        m_objCustomer.BankName = "";
                    if (!string.IsNullOrEmpty("" + grdVCustomers.GetFocusedRowCellValue("Note")))
                        m_objCustomer.Note = "" + grdVCustomers.GetFocusedRowCellValue("Note");
                    else
                        m_objCustomer.Note = "";
                    frmCustomerDetails frm = new frmCustomerDetails(m_objCustomer, m_objConnection, m_objSecurity, false);
                    frm.ShowDialog();
                    Reload();
                }
                else
                    MessageBox.Show("Vui lòng chọn 1 bản ghi cần cập nhật",
                                "Thông báo", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi xảy ra. Chi tiết: " + ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
            //2. Thiết lập thông tin cho Objects

        }

        void ucBaseControl1_PressNew(object sender, EventArgs e)
        {           
            try
            {
                this.Cursor = Cursors.WaitCursor;
                frmCustomerDetails frm = new frmCustomerDetails(null, m_objConnection, m_objSecurity, true);
                frm.ShowDialog();
                LoadCustomers();
            }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
             }
             finally
             {
                 this.Cursor = Cursors.Default;
             }             
        }

        private void LoadCustomers()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                if (m_objCusController==null)              
                    m_objCusController = new CustomerControllers(m_objConnection, m_objSecurity);
                DataTable objEmpTable = m_objEmpController.getEmployee();
                //this.uluEmployee_id.ContextImage = global::QLCONGNOAPPs.Properties.Resources.Customers_customer_32;
                this.uluEmployee_id.ContextImage = imageList1.Images[0];
                uluEmployee_id.DataSource = objEmpTable;
                DataTable objCityTable = m_objCityController.getCity();
                this.uluCityId.ContextImage = imageList1.Images[1];
                uluCityId.DataSource = objCityTable;
                m_objTable = m_objCusController.getCustomers();
                grdCCustomers.DataSource = m_objTable;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
    }
}
